﻿using Client.Base;
using Cysharp.Threading.Tasks;
using System;
using UnityEngine;
using UnityEngine.Networking;

namespace Client.Hotfix
{
    [SSM(SSMWeight.WebService)]
    public class WebService : IAwake
    {
        private readonly string url = "http://192.168.6.233:5233";
        public void Awake(params object[] args)
        {
            Debug.Log("[ Service ] - WebService is running...");
        }

        public async void Get(string url, Action<string> onSuccessfull, Action<string> onError = null) 
        {

            var request = CRequestFactory.Pop();
            request.uri = new Uri($"{url}");
            this.Log($"[web request(GET)] : {url}");
            await request.SendWebRequest().ToUniTask();
            if (request.result == UnityWebRequest.Result.Success)
            {
                string json = request.downloadHandler.text;
                //如果加密解码
                this.Log($"[web respone(GET)] : {json}");
                onSuccessfull?.Invoke(json);
            }
            else
            {
                this.Log($"[web request(GET)] : request failed . {request.uri} -> {request.error}");
                onError?.Invoke(request.error);
            }
            CRequestFactory.Push(request);
        }

        public async void GetImage(string url,Action<Texture2D> onSuccessfull, Action<string> onError = null)
        {
            var request = CRequestFactory.Pop();
            request.uri = new Uri($"{url}");
            this.Log($"[web request(GET)] : {url}");
            request.downloadHandler = new DownloadHandlerTexture();
            await request.SendWebRequest().ToUniTask();
        }


        public async void Post<T>(CWebApi cwr, Action<T> onSuccessfull, Action<string> onError = null) where T : CWebResponeData
        {
            string requestUrl = $"{url}/{cwr.api}";
            string bodyJson = cwr.body.ToJson();

            this.Log($"[api][request] : {requestUrl} -> \n{bodyJson}");

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(bodyJson);
            var request = CRequestFactory.Pop(CRequestFactory.RequestType.Post);
            request.uri = new Uri(requestUrl);
            //根据需要设置Hearders字段
            //request.SetRequestHeader("account","todo");
            //request.SetRequestHeader("password","todo");
            //request.SetRequestHeader("token","todo");
            request.SetRequestHeader("Content-Type", "application/json");
            request.uploadHandler = new UploadHandlerRaw(bytes);
            request.downloadHandler = new DownloadHandlerBuffer();
            await request.SendWebRequest().ToUniTask();
            if (request.result == UnityWebRequest.Result.Success)
            {
                string json = request.downloadHandler.text;
                //如果加密解码
                this.Log($"[api][respone] : {requestUrl} -> \n{json}");
                onSuccessfull?.Invoke(cwr.Parse<T>(json));
            }
            else
            {
                this.Log($"[api][respone] : request failed . {requestUrl} -> {request.error}");
                onError?.Invoke(request.error);
            }
            CRequestFactory.Push(request);
        }

        public async void Post<T>(string url, CWebApi cwr, Action<T> onSuccessfull, Action<string> onError = null) where T : CWebResponeData
        {
            string requestUrl = $"{url}/{cwr.api}";
            string bodyJson = cwr.body.ToJson();

            this.Log($"[api][request] : {requestUrl} -> \n{bodyJson}");

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(bodyJson);
            var request = CRequestFactory.Pop(CRequestFactory.RequestType.Post);
            request.uri = new Uri(requestUrl);
            //根据需要设置Hearders字段
            //request.SetRequestHeader("account","todo");
            //request.SetRequestHeader("password","todo");
            //request.SetRequestHeader("token","todo");
            request.SetRequestHeader("Content-Type", "application/json");
            request.uploadHandler = new UploadHandlerRaw(bytes);
            request.downloadHandler = new DownloadHandlerBuffer();
            await request.SendWebRequest().ToUniTask();
            if (request.result == UnityWebRequest.Result.Success)
            {
                string json = request.downloadHandler.text;
                //如果加密解码
                this.Log($"[api][respone] : {requestUrl} -> \n{json}");
                onSuccessfull?.Invoke(cwr.Parse<T>(json));
            }
            else
            {
                this.Log($"[api][respone] : request failed . {requestUrl} -> {request.error}");
                onError?.Invoke(request.error);
            }
            CRequestFactory.Push(request);
        }


        public async void Post(string url,Action<string> onSuccessfull, Action<string> onError = null)
        {
            string requestUrl = $"{url}";
            this.Log($"[api][request] : {requestUrl}");

            var request = CRequestFactory.Pop(CRequestFactory.RequestType.Post);
            request.uri = new Uri(requestUrl);
            //根据需要设置Hearders字段
            //request.SetRequestHeader("account","todo");
            //request.SetRequestHeader("password","todo");
            //request.SetRequestHeader("token","todo");
            request.SetRequestHeader("Content-Type", "application/json");
            await request.SendWebRequest().ToUniTask();
            request.downloadHandler = new DownloadHandlerBuffer();
            if (request.result == UnityWebRequest.Result.Success)
            {
                string json = request.downloadHandler.text;
                //如果加密解码
                this.Log($"[api][respone] : {requestUrl} -> \n{json}");
                onSuccessfull?.Invoke(json);
            }
            else
            {
                this.Log($"[api][respone] : request failed . {requestUrl} -> {request.error}");
                onError?.Invoke(request.error);
            }
            CRequestFactory.Push(request);
        }
    }
}

